 var path = require('path');

 var fs = require('fs');

 module.exports = function (UC, source, config, ArFor, cache, package) {

     var openjson = config.openjson;
     var packages = {};
     var setver = function () {
         var openpackage = function (item, Callback) {
             var packagefile = path.join(config.Files.Plugin, '/' + item.localpath + "/package.json");
             // packagefile =fs.realpathSync(packagefile);
             openjson(packagefile, function (error, db) {
                 if (error === null) {
                     db.file = packagefile;
                 }
                 Callback(db, error)
             })
         };
         this.name = "plugin-init";
         this.Type = "init";
         this.stop = function () {

         };
         var installDefolat = function (Callback) {
             config.installPackages = {};
             Callback = Callback || function () {};

             var f = function (list) {
                 list = list || {};
                 this.set = function (appid, val) {
                     list[appid] = val;
                 };
                 this.get = function (appid) {
                     return list[appid];
                 }
             }

             config.setParamet("PluginPool", new f());

             new source("M_741", {
                 
             }, function (db) {
                 if (db && db.Table) {
                     var apps = db.Table;
                     new ArFor(apps, function () {
                         if (Callback) {
                             Callback();
                         }
                     }).forEach(function (item, next) {
                         if (item.state < 3) {
                             openpackage(item, function (package, error) {
                                 if (error == null) {
                                     var file = path.join(config.Files.Plugin, '/' + item.localpath)
                                     config.InstallApp(file, package, function () {
                                         packages[package.id] = package;
                                         config.installPackages = packages;
                                         next()
                                     })
                                 } else {
                                     next()
                                 }
                             })
                         } else {
                             next()
                         }
                     })
                 } else {
                     Callback()
                 }
             })
         }
         this.init = function (Callback) {
             Callback = Callback || function () {};
             installDefolat(Callback)
         }
     }
     return new setver()
 }